package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.WorkTicketsDTO;
import com.woniuxy.mesboot.entity.MesWorktickets;
import com.woniuxy.mesboot.service.MatInProductionService;
import com.woniuxy.mesboot.service.WorkTicketsService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.TicketDetailsVO;
import com.woniuxy.mesboot.vo.WorkTicketVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 黑川
 * @version : 1.0
 * @Project : mes-boot
 * @Package : com.woniuxy.mesboot.controller
 * @ClassName : .java
 * @createTime : 2023/5/24 9:44
 * @Description :
 */
@RestController
@RequestMapping("tickets")
public class TicketsController {
    @Resource
    private WorkTicketsService ticketsService;
    @Resource
    private MatInProductionService matInProductionService;
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody WorkTicketsDTO tickets){
        PageHelper.startPage(tickets.getCurrentPage(),tickets.getPageSize(),true);
        List<WorkTicketVO> list = ticketsService.selectAll(tickets);
        PageInfo<WorkTicketVO> pageInfo = new PageInfo<>(list);
        return Result.ok().data("list",list).data("total",pageInfo.getTotal());
    }
    @GetMapping("allDetails/{ticketId}")
    public Result queryAllDetail(@PathVariable("ticketId") Integer ticketId){
        List<TicketDetailsVO> detailsVOS = ticketsService.selectAllDetails(ticketId);
        return Result.ok().data("detailsVOS",detailsVOS);
    }

//    @PostMapping
//    public Result add(@RequestBody TicketsDetailsDTO detailsDTO){
//        int i = ticketsService.insertSelective(detailsDTO);
//        if(i>0){
//            return Result.ok("添加成功");
//        }else {
//            return Result.error("添加失败");
//        }
//    }

    @GetMapping("{ticketId}")
    public Result queryById(@PathVariable Long ticketId){
        MesWorktickets worktickets = ticketsService.selectByPrimaryKey(ticketId);
        return Result.ok().data("worktickets",worktickets);
    }

    @PutMapping
    public Result update(@RequestBody MesWorktickets worktickets){
        int i = ticketsService.updateByPrimaryKeySelective(worktickets);
        if(i>0){
            return Result.ok("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @DeleteMapping("{ticketId}")
    public Result delete(@PathVariable Integer ticketId){
        int i = ticketsService.deleteByPrimaryKey(ticketId);
        if(i>0){
            return Result.ok("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
//生产
    @PutMapping("product")
    public Result product(@RequestBody WorkTicketsDTO ticketsDTO){
        int i = matInProductionService.product(ticketsDTO);
        if(i>0){
            return Result.ok("开始生产");
        }else {
            return Result.error("物料库存不足");
        }
    }
}
